package com.marktotop.controller.business;


import com.marktotop.entity.business.Supplier;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.ISupplierService;
import com.marktotop.service.IUserService;
import com.marktotop.vo.system.UserContactVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.experimental.Accessors;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器 - 供应商
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@Api(tags = "业务模块-供应商相关接口")
@RestController
@RequestMapping("/business/supplier")
public class SupplierController {


    @Autowired
    private ISupplierService supplierService;

    @Autowired
    private IUserService userService;

    /**
     * 供应商来源列表
     *
     * @return
     */
    @ApiOperation(value = "来源列表", notes = "来源列表,根据来源名模糊查询")
    @RequestMapping(value = "/findList",method = RequestMethod.GET)
    @RequiresPermissions({"supplier:show"})
    public ResultBean findSupplierList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize") Integer pageSize,
                                       Supplier supplier) {
        return supplierService.findSupplierList(pageNum, pageSize, supplier);
    }


    /**
     * 添加供应商
     *
     * @param supplier
     * @return
     */
    @ApiOperation(value = "添加供应商", notes = "添加新的供应商信息")
    @RequiresPermissions({"supplier:add"})
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultBean addSupplier(@RequestBody @Validated Supplier supplier){
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        if (!supplierService.save(supplier)){
            return ResultBean.error().message("添加供应商信息失败!");
        }
        return ResultBean.ok().message("添加供应商信息成功!");
    }


    /**
     * 获取某个供应商，带联系人信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取某个供应商", notes = "获取某个供应商具体信息")
    @RequiresPermissions({"supplier:get"})
    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    public ResultBean getOneSupplier(@PathVariable Long id) {
        Supplier supplier = supplierService.getSupplier(id);
        if (null == supplier){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().data("supplier",supplier).message("获取单个供应商信息成功!");
    }


    /**
     * 获取所有用户，作为联系人使用(全部用户)
     *
     * @return
     */
    @ApiOperation(value = "联系人列表", notes = "获取所有联系人用户列表")
    @RequestMapping(value = "/findUserForContact",method = RequestMethod.GET)
    public ResultBean findUserForContact(){
        List<UserContactVO> contacts =  userService.findUserForContact();
        if (null == contacts){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().message("获取所有联系人信息成功！").data("Contacts",contacts);
    }


    /**
     * 编辑供应商
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "编辑供应商", notes = "编辑供应商信息")
    @RequiresPermissions({"supplier:edit"})
    @RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
    public ResultBean editSupplier(@PathVariable Long id) {
        Supplier supplier = supplierService.getById(id);
        if (null == supplier){
            return ResultBean.error().message("获取要编辑的供应商信息操作失败!");
        }
        return ResultBean.ok().data("supplier",supplier).message("获取要编辑的供应商信息操作成功!");
    }


    /**
     * 更新供应商
     *
     * @param supplier
     * @return
     */
    @ApiOperation(value = "更新供应商", notes = "更新供应商信息")
    @RequiresPermissions({"supplier:update"})
    @RequestMapping(value = "/update/",method = RequestMethod.PUT)
    public ResultBean updateSupplier(@RequestBody @Validated Supplier supplier) {
        supplier.setModifiedTime(new Date());
        if (!supplierService.updateById(supplier)){
            return ResultBean.error().message("更新供应商信息操作失败!");
        }
        return ResultBean.ok().message("更新供应商信息操作成功!");
    }


    /**
     * 删除供应商
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除供应商", notes = "删除供应商信息")
    @RequiresPermissions({"supplier:delete"})
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public ResultBean deleteSupplier(@PathVariable Long id) {
        if (!supplierService.removeById(id)){
            return ResultBean.error().message("删除供应商信息操作失败!");
        }
        return ResultBean.ok().message("删除供应商信息操作成功!");
    }
}
